<?php
namespace App\Http\Controllers;
use App\Models\EquipOnoff;
use App\Models\EquipRecord;
use App\Models\EquipRecordCar;
use App\Models\Statistics;
use Carbon\Carbon;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class EquipRecordCarController extends CommonController
{
    public function index()
    {
        dump('造车运行记录');
    }
    public function getAll(Request $request)  {

    }
    // 示例方法，用于展示后台首页
    public function getlist(Request $request)
    {

    }
    public function create(Request $request)
    {
        $data = $request->all();
        $info = EquipRecordCar::where('site_id',$data['site_id'])
            ->where('equip_id',$data['equip_id'])
            ->where('project_id',$data['project_id'])
            ->whereDate('day_time',$data['day_time'])
            ->exists();
        if($info){
            return  re_warning("{$data['equip_name']}在{$data['site_name']}运行的记录已存在",null);
        }
        $recordData =$data;
        unset($recordData['onoff_list']);
        $recordData['user_ids'] = json_encode($recordData['user_ids'], true);
        $recordData['users'] = json_encode($recordData['users'], true);
        $recordData['day_time'] = $recordData['day_time'].' 00:00:00';
        $recordData['created_at'] = date('Y-m-d H:i:s');
        $recordData['status']= 1;

        DB::beginTransaction();
        try {
            //添加记录
            $res = EquipRecordCar::create($recordData);
            //添加开关机记录
            if(count($data['onoff_list'])>0){
                $onoffData = [];
                foreach ($data['onoff_list'] as $k => $v) {
                    $onoffData[] = $v;
                    $onoffData[$k]['day_time'] = $data['day_time'].' '.$v['time'].':00';
                    $onoffData[$k]['record_id'] = $res->id;
                    $onoffData[$k]['created_at'] = date('Y-m-d H:i:s');

                    $onoffData[$k]['user_id'] = $recordData['created_user_id'];
                    $onoffData[$k]['user_name'] = $recordData['created_user_name'];
                    $onoffData[$k]['reason_ids'] =$v['reason_ids']? json_encode($v['reason_ids']):null;
                    $onoffData[$k]['reasons'] = $v['reasons']?json_encode($v['reasons']):null;
                    $onoffData[$k]['user_ids'] = $v['user_ids']?json_encode($v['user_ids']):null;
                    $onoffData[$k]['users'] = $v['users']?json_encode($v['users']):null;
                    $onoffData[$k]['equip_code'] = $data['equip_code'];
                    $onoffData[$k]['equip_id'] = $data['equip_id'];
                    $onoffData[$k]['equip_name'] = $data['equip_name'];
                    $onoffData[$k]['site_id'] = $data['site_id'];
                    $onoffData[$k]['site_name'] = $data['site_name'];
                    $onoffData[$k]['check'] = 1;
                }
                EquipOnoff::insert($onoffData);
            }
            //添加数据统计

            // 提交事务
            DB::commit();
            return re_success('添加成功',null);

        }catch (\Exception $e){
            // 回滚事务
            DB::rollback();
            // 处理异常
            return re_warning('添加失败',$e->getMessage(),null);
        }

    }

    public function lastinfo(Request $request)
    {

        $data = $request->all();
        $info = EquipRecordCar::where('equip_id',$data['equip_id'])->orderBy('day_time','desc')->latest()->first();
        if($info){
            $onoff_list = EquipOnoff::where('equip_id',$info->equip_id)->where('record_id',$info->id)->get();

            $info['user_ids'] =  json_decode($info['user_ids'], true);
            $info['users'] =  json_decode($info['users'], true);
            $info['onoff_list'] =  $onoff_list;

            return re_success('获取成功',$info);
        }else{
            return re_warning('暂无',null);
        }

    }

    public function update(Request $request)
    {


    }
    public function delete(Request $request){

    }
    public function info(Request $request)
    {

    }
    public function status(Request $request)
    {

    }

}
